package com.fub.util;

import com.fub.guidance.Point;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceDAO {

	public static final String NO_NODE_SET = "-1";
	public static final String FINGERPRINT_MODE = "fingerprint_mode";
	public static final String ADD_FINGERPRINT_MODE = "add_fingerprint_mode";
	public static final String DELETE_FINGERPRINT_MODE = "delete_fingerprint_mode";
	public static final String ROUTING_ENABLED = "routingEnabled";
	public static final String AUTOMATIC_UPDATES_ENABLED = "automatic_updates";
	public static final String SHOW_GRAPH_ENABLED = "show_graph";
	public static final String SHOW_ROOMLABELS_ENABLED = "show_roomlabels";
	public static final String HANDICAPPED = "handicapped";
	public static final String ROUTING_START_ID = "routingSourceID";
	public static final String ROUTING_END_ID = "routingEndID";
	public static final String BUILDING_ID = "building_id";
	public static final String SELECTED_NODE_ID = "selected_node";
	public static final String CURRENT_Z = "z";
	public static final String CURRENT_X = "x";
	public static final String CURRENT_Y = "y";
	public static final String LAST_X = "last_x";
	public static final String LAST_Y = "last_y";
	public static final String LAST_Z = "last_z";
	public static final String SCREEN_WIDTH = "screenWidth";
	public static final String SCREEN_HEIGHT = "screenHeight";
	public static final String ZOOM = "zoom";
	public static final String SERVER_NAME = "server_name";
	public static final String STAIR_FACTOR = "stairs_factor";
	private static final int DEFAULT_X = 0;
	private static final int DEFAULT_Y = 0;
	private static final float DEFAULT_ZOOM = 0.5f;
	private static final int HVGA_WIDTH = 480;
	private static final int HVGA_HEIGHT = 320;
	public static final String DEFAULT_SERVER = "http://192.168.2.103:8080/FUBServer/";
	//public static final String DEFAULT_SERVER = "http://fublocalize.mapsforge.org/";
	public static final String DEFAULT_STAIRS_FACTOR = "2";
	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;

	public PreferenceDAO(Context context) {
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
		editor = prefs.edit();
	}

	public boolean isFingerprintEnabled() {
		return prefs.getBoolean(FINGERPRINT_MODE, false);
	}

	public boolean isAddFingerprintEnabled() {
		return prefs.getBoolean(ADD_FINGERPRINT_MODE, false);
	}

	public boolean isDeleteFingerprintEnabled() {
		return prefs.getBoolean(DELETE_FINGERPRINT_MODE, false);
	}

	public String getServerName() {
		return prefs.getString(SERVER_NAME, DEFAULT_SERVER);
	}

	public String getStairsFactor() {
		return prefs.getString(STAIR_FACTOR, DEFAULT_STAIRS_FACTOR);
	}

	public boolean isRoutingEnabled() {
		return prefs.getBoolean(ROUTING_ENABLED, false);
	}

	public boolean isShowGraphEnabled() {
		return prefs.getBoolean(SHOW_GRAPH_ENABLED, false);
	}

	public boolean isShowRoomLabels() {
		return prefs.getBoolean(SHOW_ROOMLABELS_ENABLED, true);
	}

	public boolean isAutomaticUpdate() {
		return prefs.getBoolean(AUTOMATIC_UPDATES_ENABLED, false);
	}

	public boolean isHandicapped() {
		return prefs.getBoolean(HANDICAPPED, false);
	}

	public String getRoutingStart() {
		return prefs.getString(ROUTING_START_ID, "-1");
	}

	public String getRoutingEnd() {
		return prefs.getString(ROUTING_END_ID, "-1");
	}

	public int getCurrentEtage() {
		return prefs.getInt(CURRENT_Z, 0);
	}

	public String getSelectedNodeId() {
		return prefs.getString(SELECTED_NODE_ID, NO_NODE_SET);
	}

	public void enableFingerprintMode() {
		editor.putBoolean(FINGERPRINT_MODE, true);
		editor.commit();
	}

	public void disableFingerprintMode() {
		editor.putBoolean(FINGERPRINT_MODE, false);
		editor.commit();
	}

	public void enableAddFingerprintMode() {
		editor.putBoolean(ADD_FINGERPRINT_MODE, true);
		editor.commit();
	}

	public void disableAddFingerprintMode() {
		editor.putBoolean(ADD_FINGERPRINT_MODE, false);
		editor.commit();
	}

	public void enableDeleteFingerprintMode() {
		editor.putBoolean(DELETE_FINGERPRINT_MODE, true);
		editor.commit();
	}

	public void disableDeleteFingerprintMode() {
		editor.putBoolean(DELETE_FINGERPRINT_MODE, false);
		editor.commit();
	}

	public void setEtage(int etage) {
		editor.putInt(CURRENT_Z, etage);
		editor.commit();
	}

	public void setSelectedNodeId(String selectedNode) {
		editor.putString(SELECTED_NODE_ID, selectedNode);
		editor.commit();
	}

	public void setRoutingStart(String id) {
		editor.putString(ROUTING_START_ID, id);
		editor.commit();
	}

	public void setRoutingEnd(String id) {
		editor.putString(ROUTING_END_ID, id);
		editor.commit();
	}

	public void setRoutingEnabled() {
		editor.putBoolean(ROUTING_ENABLED, true);
		editor.commit();
	}

	public void setRoutingDisabled() {
		editor.putBoolean(ROUTING_ENABLED, false);
		editor.commit();
	}

	public int getScreenWidth() {
		return prefs.getInt(SCREEN_WIDTH, HVGA_WIDTH);
	}

	public int getScreenHeight() {
		return prefs.getInt(SCREEN_HEIGHT, HVGA_HEIGHT);
	}

	public void setScreenWidth(int screenWidth) {
		editor.putInt(SCREEN_WIDTH, screenWidth);
		editor.commit();
	}

	public void setScreenHeight(int screenHeight) {
		editor.putInt(SCREEN_HEIGHT, screenHeight);
		editor.commit();
	}

	public Point getLastPosition() {
		int x = prefs.getInt(LAST_X, DEFAULT_X);
		int y = prefs.getInt(LAST_Y, DEFAULT_Y);
		int z = getCurrentEtage();
		return new Point(x, y, z);
	}

	public void setLastPosition(int x, int y, int z) {
		editor.putInt(LAST_X, x);
		editor.putInt(LAST_Y, y);
		editor.putInt(LAST_Z, z);
		editor.commit();
	}

	public void setLastPosition(Point position) {
		setLastPosition(position.getX(), position.getY(), position.getZ());
	}

	public Point getPosition() {
		int x = prefs.getInt(CURRENT_X, DEFAULT_X);
		int y = prefs.getInt(CURRENT_Y, DEFAULT_Y);
		int z = getCurrentEtage();
		return new Point(x, y, z);
	}

	public void setPosition(Point point) {
		setPosition(point.getX(), point.getY(), point.getZ());
	}

	public void setPosition(int x, int y, int z) {
		editor.putInt(CURRENT_X, x);
		editor.putInt(CURRENT_Y, y);
		editor.putInt(CURRENT_Z, z);
		editor.commit();
	}

	public String getBuildingId() {
		return prefs.getString(BUILDING_ID, "noBuilding");
	}

	public void setBuildingId(String buildingId) {
		editor.putString(BUILDING_ID, buildingId);
		editor.commit();
	}

	public float getZoomFactor() {
		return prefs.getFloat(ZOOM, DEFAULT_ZOOM);
	}

	public void setZoomFactor(float zoom) {
		editor.putFloat(ZOOM, zoom);
		editor.commit();
	}

	public Point getLeftUpperCorner() {
		return new Point(getPosition().getX() - getScreenWidth() / 2,
				getPosition().getY() - getScreenHeight() / 2, getCurrentEtage());
	}

	public Point getRightBottomCorner() {
		return new Point(getPosition().getX() + getScreenWidth() / 2,
				getPosition().getY() + getScreenHeight() / 2, getCurrentEtage());
	}
}
